﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace XMM.Objects
{
    public class DiskMedia : Object
    {
        /// <summary>
        /// DiskMedia Database-ID.
        /// </summary>
        public int DiskMediaID { get; set; }

        /// <summary>
        /// Name of the disk.
        /// </summary>
        public string MediaName { get; set; }

        /// <summary>
        /// Date of addition to database.
        /// </summary>
        public DateTime MediaDate { get; set; }

        /// <summary>
        /// Count of aquired Media referenced to this DiskMedia.
        /// </summary>
        public int AquiredMedia
        {
            get
            {
                return getAquiredMedia();
            }
        }

        //public event ;

        /// <summary>
        /// Count aquired media for this DiskMedia.
        /// </summary>
        /// <returns>(int) Count of aquired media with current DiskMediaID.</returns>
        private int getAquiredMedia()
        {
            int iAquiredMediaCount = 0;

            try
            {
                XMMDBDataContext dbConnection = new XMMDBDataContext();

                // Count movies
                iAquiredMediaCount += (from media in dbConnection.t_movies where media.DiskMediaID == DiskMediaID select media.ID).Count();
            }
            catch (SqlException ex)
            {
                System.Windows.MessageBox.Show("Fehler beim Zählen der erfassten Medien für Datenträger \"" + MediaName + "\": " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }

            return iAquiredMediaCount;
        }

        /// <summary>
        /// Initialize a blank DiskMedia()-Object.
        /// </summary>
        public DiskMedia()
        {
            blankValues();
        }

        /// <summary>
        /// Initialize a new DiskMedia()-Object and load values from database.
        /// </summary>
        /// <param name="iMediaID">Target DiskMedia-Database-ID.</param>
        public DiskMedia(int iMediaID)
        {
            blankValues();

            Load(iMediaID);
        }

        /// <summary>
        /// Reset all values.
        /// </summary>
        public void blankValues()
        {
            DiskMediaID = 0;
            MediaName = String.Empty;
            MediaDate = DateTime.Now;
        }

        public override string ToString()
        {
            return MediaName;
        }

        /// <summary>
        /// Load specific DiskMedia.
        /// </summary>
        /// <param name="iMediaID">Target DiskMedia-ID.</param>
        /// <returns>[true] if DiskMedia was found and loaded successfully.</returns>
        public bool Load(int iMediaID)
        {
            if (iMediaID != 0)
            {
                try
                {
                    XMMDBDataContext dbConnection = new XMMDBDataContext();

                    t_disk_media vDiskMedia = (from diskmedia in dbConnection.t_disk_media where diskmedia.ID == iMediaID select diskmedia).Single();

                    if (vDiskMedia != null && vDiskMedia.ID != 0)
                    {
                        DiskMediaID = vDiskMedia.ID;
                        MediaName = vDiskMedia.MediaName;
                        MediaDate = vDiskMedia.MediaDate;

                        return true;
                    }
                }
                catch (SqlException ex)
                {
                    System.Windows.MessageBox.Show("Fehler beim Laden des Datenträgers '" + iMediaID.ToString() + "': " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                }
            }
            else
            {
                blankValues();
            }
            
            return false;
        }

        /// <summary>
        /// Save new DiskMedia()-object to database.
        /// </summary>
        /// <returns>[true] if DB-Record was created successfully.</returns>
        public bool SaveNew()
        {
            if (MediaName != String.Empty)
            {
                try
                {
                    XMMDBDataContext dbConnection = new XMMDBDataContext();

                    t_disk_media vDiskMedia = new t_disk_media();
                    vDiskMedia.MediaName = MediaName;
                    vDiskMedia.MediaDate = DateTime.Now;

                    dbConnection.t_disk_media.InsertOnSubmit(vDiskMedia);
                    dbConnection.SubmitChanges();

                    return true;
                }
                catch (SqlException ex)
                {
                    System.Windows.MessageBox.Show("Fehler beim Speichern des Datenträgers \"" + MediaName + "\": " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                }
            }

            return false;
        }

        public static List<Objects.DiskMedia> getAllDiskMedia()
        {
            List<Objects.DiskMedia> lAvailableMedia = new List<DiskMedia>();

            try
            {
                XMMDBDataContext dbConnection = new XMMDBDataContext();

                var vDiskMediaIDs = from diskmedia in dbConnection.t_disk_media select diskmedia.ID;

                if (vDiskMediaIDs != null && vDiskMediaIDs.Count() > 0)
                {
                    foreach (int iDiskMediaID in vDiskMediaIDs)
                    {
                        Objects.DiskMedia newDisk = new DiskMedia(iDiskMediaID);
                        if (newDisk != null && newDisk.DiskMediaID != 0)
                        {
                            lAvailableMedia.Add(newDisk);
                        }
                    }
                }
            }
            catch (SqlException ex)
            {
                System.Windows.MessageBox.Show("Fehler beim Abrufen aller Sammel-Datenträger: " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }


            return lAvailableMedia;
        }

        public static int getLastDiskMediaID()
        {
            int iLastDiskID = 0;

            try
            {
                XMMDBDataContext dbConnection = new XMMDBDataContext();

                iLastDiskID = (from diskid in dbConnection.t_disk_media select diskid.ID).Max();
            }
            catch (SqlException ex)
            {
                System.Windows.MessageBox.Show("Fehler beim Abrufen der neuesten Sammel-Datenträger-ID: " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }

            return iLastDiskID;
        }

        public bool Delete()
        {
            if (DiskMediaID != 0)
            {
                try
                {
                    XMMDBDataContext dbConnection = new XMMDBDataContext();

                    t_disk_media vDiskMedia = (from diskmedia in dbConnection.t_disk_media where diskmedia.ID == DiskMediaID select diskmedia).Single();

                    if (vDiskMedia != null && vDiskMedia.ID != 0)
                    {
                        dbConnection.t_disk_media.DeleteOnSubmit(vDiskMedia);
                        dbConnection.SubmitChanges();
                        blankValues();

                        return true;
                    }
                }
                catch (SqlException ex)
                {
                    System.Windows.MessageBox.Show("Fehler beim Löschen von Sammel-Datenträger \"" + MediaName + "\": " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                }
            }

            return false;
        }

        /// <summary>
        /// Save changes to existing DiskMedia-Item.
        /// </summary>
        /// <returns></returns>
        public bool SaveChanges()
        {
            if (DiskMediaID != 0)
            {
                if (MediaName != String.Empty)
                {
                    try
                    {
                        XMMDBDataContext dbConnection = new XMMDBDataContext();

                        t_disk_media vDiskMedia = (from diskmedia in dbConnection.t_disk_media where diskmedia.ID == DiskMediaID select diskmedia).Single();

                        if (vDiskMedia != null && vDiskMedia.ID != 0)
                        {
                            vDiskMedia.MediaName = MediaName;
                            vDiskMedia.MediaDate = DateTime.Now;

                            dbConnection.SubmitChanges();

                            return true;
                        }
                    }
                    catch (SqlException ex)
                    {
                        System.Windows.MessageBox.Show("Fehler beim Speichern der Änderungen an Datenträger '" + DiskMediaID.ToString() + "' (" + MediaName + "): " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                    }
                }
            }

            return false;
        }
    }
}
